package com.oasistudio.ads.expert.banner;

import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import cn.appmedia.ad.AdManager;
import cn.appmedia.ad.AdViewListener;
import cn.appmedia.ad.BannerAdView;

import com.oasistudio.ads.expert.R;

public class AdViewAppMedia extends BannerAdView{
	AppMediaAdRequestListener adRequestListener;
	Activity activity;
	public static final String UNION = "appmedia.cn";
	public AdViewAppMedia(Activity activity){
		super(activity);
		this.activity = activity;
		this.adRequestListener = new AppMediaAdRequestListener(activity, UNION);
		addAppMediaAd();
	}
	
	@Override
	public void requestAd(){
		this.adRequestListener.beforeAdRequest();
		super.requestAd();
	}
	
	/************************** www.appmedia.cn start *************************/
	// private final static String AID = "9dd924ee8579a15c";//test ID
	private final static String AID = "a0a1612985eb8233";//my ID
	static {
		AdManager.setAid(AID);
	}

	private void addAppMediaAd() {
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));

		LinearLayout appMediaContainer = new LinearLayout(activity);
		adContainer.addView(appMediaContainer);

		this.setAdTextColor(0xffffffff);
		this.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println(".............AdViewAppMedia.adView.onClick().............");
				AdViewAppMedia.this.adRequestListener.adClicked();
			}
		});//记录广告点击
		appMediaContainer.addView(this, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		this.setAdListener(this.adRequestListener);
	}

	/************************** www.appmedia.cn end *************************/

	class AppMediaAdRequestListener extends AbstractAdsExpertAdListener implements AdViewListener{
		public AppMediaAdRequestListener(Activity activity, String union){
			super(activity, union);
		}

		@Override
		public void onReceiveAdFailure(BannerAdView arg0) {
			// TODO Auto-generated method stub
			this.fail();
		}

		@Override
		public void onReceiveAdSuccess(BannerAdView adView) {
			// TODO Auto-generated method stub
			this.succeed();
			AdHistory.saveAd(UNION, adView);
		}

	}

}
